<template>
  <div>
    <div class="search-term">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
        <el-form-item label="键名">
          <el-input v-model="searchInfo.key" placeholder="搜索条件" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="openDialog">新增对象</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      border
      stripe
      style="width: 100%"
      tooltip-effect="dark"
    >

      <el-table-column type="selection" width="55" />

      <el-table-column label="描述" prop="description" width="360" />

      <el-table-column label="键名" prop="key" width="240" />

      <el-table-column label="键值" prop="value" />

      <el-table-column label="操作" width="145">
        <template slot-scope="scope">
          <el-button size="small" type="primary" style="margin-right: 10px" @click="updateSysObj(scope.row)">变更</el-button>
          <el-popconfirm
            title="确认删除？"
            confirm-button-text="确定"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            placement="top-end"
            @confirm="deleteSysObj(scope.row)"
            @cancel="scope.row.visible = false"
          >
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="page"
      :page-size="pageSize"
      :page-sizes="[10, 30, 50, 100]"
      :style="{float:'right',padding:'20px'}"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />

    <el-dialog :before-close="closeDialog" :visible.sync="dialogFormVisible" title="弹窗操作" width="40%">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-position="top">
        <el-form-item label="键名" prop="key">
          <el-input v-model="formData.key" placeholder="请输入键名（英）" clearable />
        </el-form-item>
        <el-form-item label="键值" prop="name">

          <el-select
            v-model="valueList"
            :style="{width: '100%'}"
            :filterable="true"
            :multiple="true"
            :allow-create="true"
            placeholder="请选择"
          >
            <el-option
              v-for="item in formData.value.split(';')"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>

        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="formData.description" placeholder="请输入描述" clearable :style="{width: '100%'}" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="enterDialog">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import {
  getSysObjList,
  findSysObj,
  createSysObj,
  updateSysObj,
  deleteSysObj
} from '@/api/sysObj'
import infoList from '@/mixins/infoList'
export default {
  name: 'SysObj',

  filters: {

  },
  mixins: [infoList],
  data() {
    return {
      listApi: getSysObjList,
      dialogFormVisible: false,
      visible: false,
      type: '',
      valueList: [],
      formData: {
        key: null, value: '', description: null
      },
      rules: {
        key: [{
          required: true,
          message: '请输入键名（英）',
          trigger: 'blur'
        }],
        value: [{
          required: true,
          message: '键值',
          trigger: 'blur'
        }],
        description: [{
          required: true,
          message: '请输入描述',
          trigger: 'blur'
        }]
      }
    }
  },
  async created() {
    this.getTableData()
  },
  methods: {
    // 条件搜索前端看此方法
    onSubmit() {
      this.page = 1
      this.pageSize = 10
      this.getTableData()
    },
    async updateSysObj(row) {
      const res = await findSysObj({ ID: row.ID })
      this.type = 'update'
      if (res.code === 0) {
        this.formData = res.data.resysObj
        this.valueList = this.formData.value.split(';')
        this.dialogFormVisible = true
      }
    },
    closeDialog() {
      this.dialogFormVisible = false
      this.formData = {
        key: null,
        value: '',
        description: null
      }
    },
    async deleteSysObj(row) {
      this.visible = false
      const res = await deleteSysObj({ ID: row.ID })
      if (res.code === 0) {
        this.$notify({
          type: 'success',
          title: '成功',
          message: '删除成功'
        })
        this.getTableData()
      }
    },
    async enterDialog() {
      this.$refs['elForm'].validate(async valid => {
        if (!valid) return
        let res
        this.formData.value = this.valueList.join(';')
        switch (this.type) {
          case 'create':
            res = await createSysObj(this.formData)
            break
          case 'update':
            res = await updateSysObj(this.formData)

            break
          default:
            res = await createSysObj(this.formData)
            break
        }
        if (res.code === 0) {
          this.$notify({
            type: 'success',
            title: '成功',
            message: '操作成功'
          })
          this.closeDialog()
          this.getTableData()
        }
      })
    },
    openDialog() {
      this.type = 'create'
      this.valueList = []
      this.dialogFormVisible = true
    }
  }
}
</script>

<style>
</style>
